using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Base;
using System.Collections;

namespace Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel.Mappings.Helper
{
    /// <summary/>
    internal static class ClonerHelper
    {
        /// <summary>
        /// Clones the property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        public static Property CloneProperty(Property property)
        {
            Property clonedProperty = new Property();

            clonedProperty.ColumnName = property.ColumnName;
            clonedProperty.IsNullable = property.IsNullable;
            clonedProperty.IsPrimaryKey = property.IsPrimaryKey;
            clonedProperty.Name = property.Name;
            clonedProperty.ReadOnly = property.ReadOnly;
            clonedProperty.Type = property.Type;

            return clonedProperty;
        }

        /// <summary>
        /// Clones the parameter.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns></returns>
        public static Parameter CloneParameter(Parameter parameter)
        {
            Parameter clonedParameter = new Parameter();
            DbDataType clonedDbDataType = new DbDataType();

            clonedDbDataType.Precision = parameter.DbDataType.Precision;
            clonedDbDataType.ProviderType = parameter.DbDataType.ProviderType;
            clonedDbDataType.Scale = parameter.DbDataType.Scale;
            clonedDbDataType.Size = parameter.DbDataType.Size;
            clonedDbDataType.Type = parameter.DbDataType.Type;

            clonedParameter.DbDataType = clonedDbDataType;
            clonedParameter.Direction = parameter.Direction;
            clonedParameter.DBName = parameter.DBName;

            return clonedParameter;
        }

        /// <summary>
        /// Clones the entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static Entity CloneEntity(Entity entity)
        {
            Entity clonedEntity = new Entity();

            clonedEntity.Name = entity.Name;            

            foreach(Property property in entity.Properties)
            {
                Property clonedProperty = CloneProperty(property);

                clonedEntity.Properties.Add(clonedProperty);
            }

            clonedEntity.TableName = entity.TableName;
            clonedEntity.Namespace = entity.Namespace;

            return clonedEntity;
        }

        /// <summary>
        /// Clones the entities.
        /// </summary>
        /// <param name="entities">The entities.</param>
        /// <returns></returns>
        public static ArrayList CloneEntities(ArrayList entities)
        {
            ArrayList clonedEntities = new ArrayList();

            foreach(Entity entity in entities)
            {
                clonedEntities.Add(ClonerHelper.CloneEntity(entity));
            }

            return clonedEntities;
        }
    }
}
